package com.wkh.project.handle;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wkh.project.common.BaseResponse;
import com.wkh.project.common.ErrorCode;
import com.wkh.project.common.ResultUtils;
import com.wkh.project.model.dto.questionBank.QuestionBankQueryRequest;
import com.wkh.project.model.vo.QuestionBankVO;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author wangkehua
 */
public class QuestionBankHandle {
    /**
     * listQuestionBankVOByPage 降级操作：直接返回本地数据
     *
     * 处理业务逻辑中出现的错误
     */
    public static BaseResponse<Page<QuestionBankVO>> handleFallback(@RequestBody QuestionBankQueryRequest questionBankQueryRequest,
                                                             HttpServletRequest request, Throwable ex) {
        // 可以返回本地数据或空数据
        return ResultUtils.success(null);
    }

    /**
     * listQuestionBankVOByPage 流控操作
     * 限流：提示“系统压力过大，请耐心等待”
     *
     * 可以处理熔断之后的降级操作和处理限流的操作
     */
    public static BaseResponse<Page<QuestionBankVO>> handleBlockException(@RequestBody QuestionBankQueryRequest questionBankQueryRequest,
                                                                          HttpServletRequest request, BlockException ex) {
        //如果是降级异常
        if (ex instanceof DegradeException){
            return handleFallback(questionBankQueryRequest,request,ex);
        }
        // 限流操作
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "系统压力过大，请耐心等待");
    }

}
